

cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Replication\Log files\Pseudo"
clear
log using Table3_Pseudo.txt, text replace

***Keeping key variables only and replacing them for pesudo variables
***Main results
clear all
cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Data"
use LPSU_MainSample_Sep2024, replace
set matsize 10000


**Create ID that cannont be traced
set seed 155
bysort id: gen random_number = runiform()
bysort id (random_number): gen random_id_temp = random_number[1]
egen new_id = group(random_id_temp)
order id year random_number random_id_temp new_id*
sort new_id year
drop random_number random_id_temp id
rename new_id id
label var id "Firm-attempt id (pseudo)"

**Creating pseudo OROA
*Pseudo OROA is OROA+ a N(0,5%)
set seed 1303
gen norm_var = rnormal()
gen new_var = 0 + 0.05 * norm_var
winsor new_var, p(0.01) gen(new_var_w)

gen wroa_ps=wroa+ new_var_w
label var wroa_ps "OROA (pseudo)"

tabstat wroa_ps  wroa, ///
 statistics( mean p10 p25 p50 p75 p90 sd count ) columns(statistics) save
 mat tab2= r(StatTotal)'
frmttable using table1_sumstat.doc, statmat(tab2)  sdec(2,2,2,2,2,2,2,0) varlabels replace  ctitle("","Mean", "P10" , "P25", "P50", "P75", "P90", "SD", "Total")

**Dropping original OROA and auxiliary variable
drop norm_var new_var* wroa


**Groups for FEs
egen sic1year=group(sic1 year)
egen month_post= group(month post)

**Replace sic1year for a new identifier
set seed 1303
bysort sic1year: gen random_number = runiform()
bysort sic1year (random_number): gen random_id_temp = random_number[1]
egen new_id = group(random_id_temp)
**Dropping  auxiliary variables
drop random_number random_id_temp sic1year
rename new_id sic1year
label var sic1year "Sic-year (pseudo)"

**Replace month_post for a new identifier
set seed 1303
bysort month_post: gen random_number = runiform()
bysort month_post (random_number): gen random_id_temp = random_number[1]
egen new_id = group(random_id_temp)
**Dropping  auxiliary variables
drop random_number random_id_temp month_post
rename new_id month_post
label var month_post "Month-post (pseudo)"

*keep only relevant variables
keep id wroa_ps IPO year_around_ipo instrument placebo_instrument sic1year month_post 
sort id year
order id year year_around_ipo IPO instrument placebo_instrument wroa_ps

cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Replication\Data"

save LPSU_PseudoSample_Sep2024, replace



***Replicating results

clear all
cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Replication\Data"
use LPSU_PseudoSample_Sep2024, replace
set matsize 10000
cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Tables"

distinct id

***Set of fixed effects
qui tabulate sic1year, generate(dsic1year)
qui tabulate month_post, generate(dmonth)
qui tabulate year_around_ipo, generate(deventyear) 


***Setting the sample to the IV sample (dropping colinear variables with FEs)
set more off
qui xtivreg2 wroa_ps (IPO=instrument) deventyear2 deventyear3 deventyear4 deventyear5 dsic1year* dmonth* , fe first cluster(id) partial(deventyear2 deventyear3 deventyear4 deventyear5 dsic1year* dmonth*)
gen sample_IV=e(sample) 
keep if sample_IV==1
**Dropping firms for which ROA only has one observation
bysort id: egen count=count(year)
drop if count==1 

set more off
eststo:qui xtreg wroa_ps IPO deventyear2 deventyear3 deventyear4 deventyear5  dsic1year* dmonth* , fe cluster(id) 
outreg2 using Table_main, keep(IPO) nocons label  word bdec(3) tex(frag) addtext(Firm FE, Yes, Event year FE, Yes, SIC x calendar year FE, Yes, IPO month x Post FE, Yes, Regression, OLS,Instrument,-) replace

set more off
eststo:qui xtreg IPO instrument deventyear2 deventyear3 deventyear4 deventyear5 dsic1year* dmonth*, fe cluster(id) 
test instrument
outreg2 using Table_main, keep(instrument) sortvar(IPO instrument placebo_instrument) tex(frag) word bdec(3) nocons label ///
 addtext(Firm FE, Yes, Event year FE, Yes, SIC x calendar year FE, Yes, IPO month x Post FE, Yes, Regression, First stage,Instrument,-)   addstat("F test model", r(F)) 

set more off 
eststo:qui xtreg IPO placebo_instrument deventyear2 deventyear3 deventyear4 deventyear5 dsic1year* dmonth*, fe cluster(id) 
test placebo_instrument
outreg2 using Table_main, keep(placebo_instrument) sortvar(IPO instrument placebo_instrument) tex(frag) word bdec(3) nocons label ///
 addtext(Firm FE, Yes, Event year FE, Yes, SIC x calendar year FE, Yes, IPO month x Post FE, Yes, Regression, Placebo First stage,Instrument,-) addstat("F test model", r(F))  

set more off
eststo:qui xtivreg2 wroa_ps (IPO=instrument) deventyear2 deventyear3 deventyear4 deventyear5 dsic1year* dmonth* , fe first cluster(id) partial(deventyear2 deventyear3 deventyear4 deventyear5 dsic1year* dmonth*)
outreg2 using Table_main, keep(IPO) sortvar(IPO instrument placebo_instrument) tex(frag) word bdec(3) nocons nor2 label ///
 addtext(Firm FE, Yes, Event year FE, Yes, SIC x calendar year FE, Yes,  IPO month x Post FE, Yes,Regression, Second stage,Instrument,Pre-IPO ret>0) 
 
 set more off
eststo:qui xtreg wroa_ps instrument deventyear2 deventyear3 deventyear4 deventyear5  dsic1year* dmonth*, fe cluster(id) 
outreg2 using Table_main, keep(instrument) sortvar(IPO instrument placebo_instrument) tex(frag) word bdec(3) nocons label ///
 addtext(Firm FE, Yes, Event year FE, Yes, SIC x calendar year FE, Yes, IPO month x Post FE, Yes, Regression, Reduced form,Instrument,-) 
 
 
set more off
eststo:qui xtreg wroa_ps placebo_instrument deventyear2 deventyear3 deventyear4 deventyear5 dsic1year* dmonth* ,  fe  cluster(id)   
outreg2 using Table_main, keep(placebo_instrument) sortvar(IPO instrument placebo_instrument) tex(frag) word bdec(3) nocons label ///
 addtext(Firm FE, Yes, Event year FE, Yes, SIC x calendar year FE, Yes, IPO month x Post FE, Yes,Regression, Placebo reduced form,Instrument,-)  

**Note: R^2 does not apply to IV regressions 
esttab,   keep(IPO instrument placebo_instrument) order(IPO instrument placebo_instrument) label se b(%9.3f) se(%9.3f) r2 star(* 0.1 ** 0.05 *** 0.01) addnotes(Firm FE, Yes, Event year FE, Yes, SIC x calendar year FE, Yes, IPO month x Post FE, Yes, r^2 for IV regression has no interpretation) 
 
 
eststo clear 


cd "C:\Users\sertsios\Dropbox\LPSU replication package RFS\Replication\Log files\Pseudo"
log close

